<?php
class Simpple_Controller_Default extends Simpple_Controller_Abstract
{
	/**
	 * 视图
	 *
	 * @var Simpple_View_Default
	 */
	protected $view;
	
	/**
	 * 请求
	 *
	 * @var Simpple_Controller_Request
	 */
	protected $request;
	
	private $priviousControllers = array();
	
	/**
	 * 初始化，可覆盖
	 *
	 */
	public function init() 
	{
		
	}
	
	/**
	 * 初始化视图
	 * @param string $path 视图路径
	 */
	public function initView($path) 
	{
		require_once 'Simpple/View/Default.php';
		$this->view = new Simpple_View_Default();
		
		// 设置脚本路径
		$defaultPath = $path.'/views/scripts/';
		$this->view->addScriptPath($defaultPath);
	}
	
	/**
	 * 接收一个控制器
	 * 
	 * @param Simpple_Controller_Abstract
	 */
	public function receiveController(Simpple_Controller_Abstract $ctl) 
	{
		$this->priviousControllers[] = $ctl;
	}
	
	/**
	 * 获取上一个控制器
	 *
	 * @return Simpple_Controller_Abstract
	 */
	protected function getPreviousController()
	{
		return current($this->priviousControllers);
	}
	
	/**
	 * 设置请求
	 *
	 * @param Simpple_Controller_Request $request
	 */
	public function setRequest($request) 
	{
		$this->request = $request;
	}
	
	/**
	 * 获取请求
	 *
	 * @return Simpple_Controller_Request
	 */
	public function getRequest() 
	{
		return $this->request;
	}
	
	/**
	 * 获取参数
	 *
	 * @param string $key
	 * @param mixed $defVal
	 * @return mixed
	 */
	public function _getParam($key, $defVal=null) 
	{
		return $this->request->getParam($key, $defVal);
	}
	
	/**
	 * 分配变量
	 *
	 * @param string $key
	 * @param mixed $value
	 */
	public function _assign($key, $value) 
	{
		$this->view->assign($key, $value);
	}
	
	/**
	 * 显示模板
	 *
	 * @param string $tpl
	 */
	public function _display($tpl = '')
	{
		if (!$tpl) {
			$tpl = $this->request->getControllerName().'/'.$this->request->getActionName();
		}
		$this->view->display($tpl);
	}
	
	/**
	 * 当调用找不到的action时调用的方法
	 *
	 * @param string $param
	 */
	public function __action($action) 
	{
		return $action;
	}
	
	/**
	 * 当发生错误时调用的方法
	 *
	 * @param unknown_type $ex
	 */
	public function __error($ex) 
	{
		throw $ex;
	}
}